我正在嘗試使用閃亮建立交互式散點圖。使用虹膜數據,我想讓用戶選擇散點圖的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