cannot coerce type 'closure' to vector of type 'character'
我正在尝试使用光泽构建交互式散点图。使用虹膜数据,我想让用户选择散点图的x和y尺寸(花瓣与萼片),然后输出所选尺寸的简单散点图。非常简单。
首先,我需要构建一个函数,使我可以将表示尺寸的字符串传递给ggplot。我这样做并用静态数据对其进行了测试。工作良好。
接下来,我为花瓣和萼片尺寸(这是我的x和y轴)定义两个下拉列表和两个后续字符串(使用发亮)。
接下来,我使用带有switch语句的闪亮的react()函数设置两个字符串变量。
这似乎是出问题的地方。
我得到的错误是:错误:无法将类型'closure'强制转换为类型为'character'的vector
我已经采取了许多步骤来调试我的代码。我首先将硬编码尺寸(例如" Petal.Length")插入到代码输出的最后一行中$$ myplot = renderPlot({myplotfunct(...
这很好。该图呈现了我期望的样子。
然后,我添加了一条调试行以跟踪通过此绘图函数传递的字符串的值。答对了。它是空的。为什么它是空的?似乎应该从UI.r文件中传递一个值。
代码如下。
任何帮助将不胜感激。谢谢!
用户界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | library(shiny) # Define UI for dataset viewer application shinyUI(fluidPage( # Application title titlePanel("Shiny Text"), # Sidebar with controls to select a dataset and specify the # number of observations to view sidebarLayout( sidebarPanel( selectInput("dataset1","Choose a Sepal Measure:", choices = c("Sepal Length","Sepal Width")), selectInput("dataset2","Choose a Petal Measure:", choices = c("Petal Length","Petal Width")) ), # Main Scatter Plot mainPanel( textOutput("testvar"), plotOutput("myplot") ) ) )) |
服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | library(shiny) library(datasets) library(ggplot2) #Define a function to plot passed string variables in ggplot myplotfunct = function(df, x_string, y_string) { ggplot(df, aes_string(x = x_string, y = y_string)) + geom_point() } shinyServer(function(input, output) { # Sepal Inputs datasetInput1 <- reactive({ switch(input$dataset1, "Sepal Length" ="Sepal.Length", "Sepal Width" ="Sepal.Width") }) # Petal Inputs datasetInput2 <- reactive({ switch(input$dataset2, "Petal Length" ="Petal.Length", "Petal Width" ="Petal.Width") }) #Debug print value of sting being passed output$testvar = renderText(print(datasetInput1)) # Plot output$myplot = renderPlot({myplotfunct(iris, datasetInput1, datasetInput2)}) }) |
最后两行中对datasetInput1和datasetInput2的调用是错误的原因。
您应该改为调用datasetInput1()和datasetInput2()。
否则,R尝试将函数转换为char。
只需添加(),错误就会消失,如下所示: