我正在尝试使用闪亮建立交互式散点图。使用虹膜数据,我想让用户选择散点图的x和y尺寸(花瓣vs萼片),然后输出所选尺寸的简单散点图。非常简单。不能强制类型'关闭'到类型'字符'的向量
首先,我需要构建一个函数,允许我将代表尺寸的字符串传递给ggplot。我做了这个,并用静态数据对它进行了测试。工作正常。
接下来,我为花瓣和萼片尺寸(这些是我的x和y轴)定义了两个下拉和两个后续字符串(使用闪亮)。
我接下来使用一个switch语句,使用shiny的reactive()函数设置两个字符串变量。
这似乎是事情出错的地方。
我得到的错误是:错误:不能强迫式“封闭”类型为“字符”
我已经采取了一些步骤来调试我的代码的载体。我第一次插入硬编码的尺寸(例如“Petal.Length”)到我的代码输出的最后一行$ myplot = renderPlot({myplotfunct(...
这很好,剧情呈现为我期望的。
我加入调试行跟踪我通过这个情节函数的字符串的值。宾果游戏,它是空的。为什么空?好像应该通过从UI的值。 R档。
代码如下。
任何帮助将不胜感激。谢谢!
UI.R
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")
)
)
))
Server.R
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()'在'myplotfunct()'调用? – tospig