ggplotly在RStudio控制台中工作,但在闪亮的应用程序中不渲染
我正在创建一个闪亮的应用程序,我写了一个函数来显示一个绘图。当我在控制台中运行该函数时,该函数工作正常并打印绘图,但当我运行应用程序时,ggplotly直方图将不会呈现。在控制台中运行该功能或尝试运行应用程序时,我没有收到任何错误。这些图表不会显示在应用程序中。下面是我在助手文件中编写的函数:ggplotly在RStudio控制台中工作,但在闪亮的应用程序中不渲染,r,ggplot2,shiny,plotly,R,Ggplot2,Shiny,Plotly,我正在创建一个闪亮的应用程序,我写了一个函数来显示一个绘图。当我在控制台中运行该函数时,该函数工作正常并打印绘图,但当我运行应用程序时,ggplotly直方图将不会呈现。在控制台中运行该功能或尝试运行应用程序时,我没有收到任何错误。这些图表不会显示在应用程序中。下面是我在助手文件中编写的函数: # making function to display simulated state-level pv2ps pv2p_plot <- function(x) { # filter ba
# making function to display simulated state-level pv2ps
pv2p_plot <- function(x) {
# filter based on input$state from ui.R
# getting text to specify the predicted pv2p and the chance of victory
pv2p <- sims %>%
drop_na() %>%
filter(state == x) %>%
mutate(d_pv2p = sim_dvotes_2020 / (sim_rvotes_2020 + sim_dvotes_2020),
r_pv2p = 1 - d_pv2p) %>%
summarise(d_pv2p = mean(d_pv2p) * 100,
r_pv2p = mean(r_pv2p) * 100)
win_prob <- sims %>%
mutate(biden_win = ifelse(sim_dvotes_2020 > sim_rvotes_2020, 1, 0)) %>%
group_by(state) %>%
summarise(pct_biden_win = mean(biden_win, na.rm = TRUE)) %>%
filter(pct_biden_win < 1 & pct_biden_win > 0) %>%
mutate(pct_trump_win = 1 - pct_biden_win) %>%
select(state, pct_biden_win, pct_trump_win) %>%
filter(state == x)
pv2p_lab <- paste0("Forecasted Two-Party Popular Vote: ", round(pv2p$d_pv2p, 2), "% for Biden and ", round(pv2p$r_pv2p, 2), "% for Trump")
win_lab <- paste0("Forecasted Probability of Electoral College Victory: ", round(win_prob$pct_biden_win * 100, 2), "% for Biden and ", round(win_prob$pct_trump_win * 100, 2), "% for Trump")
pv_plot <- sims %>%
filter(state == x) %>%
mutate(Democrat = sim_dvotes_2020 / (sim_dvotes_2020 + sim_rvotes_2020),
Republican = 1 - Democrat) %>%
pivot_longer(cols = c(Democrat, Republican), names_to = "party") %>%
ggplot(aes(value, fill = party)) +
geom_histogram(aes(y = after_stat(count / sum(count)),
text = paste0("Probability: ", round(after_stat(count / sum(count)), 5))), bins = 1000, alpha = 0.5, position = "identity") +
scale_fill_manual(breaks = c("Democrat", "Republican"),
labels = c("Biden", "Trump"),
values = c(muted("blue"), "red3")) +
labs(title = paste("Simulated Two-Party Popular Vote \nin", x),
x = "Predicted Share of the Two-Party Popular Vote",
y = "Probability",
fill = "Candidate",
subtitle = pv2p_lab) +
theme_hodp()
print(ggplotly(pv_plot, tooltip = "text"))
}
这是我在应用程序中的UI和服务器代码:
# loaded libraries, read in data, and created functions in other file to keep
# this script nice and clean
source("helper.R")
ui <- navbarPage(
# Application title
"Presidential Forecast in Retrospect",
tabPanel(
"About",
includeHTML(file.path("pages/about.html"))
),
navbarMenu("Forecast Simulations",
tabPanel("State-by-State Two-Party Popular Vote",
fluidPage(theme = "bootstrap.css",
tabsetPanel(
tabPanel("Estimated Vote Share",
selectInput("state",
"State:",
sims %>% pull(state) %>% unique() %>% sort()),
plotlyOutput("statesimPlotly")),
tabPanel("Probability of Victory",
selectInput("state_type",
"State Category:",
types %>% pull(type) %>% unique()),
plotlyOutput("statevictoryPlotly")
)
)
)
),
tabPanel("Predicted Vote Margin Map",
# creating this page to show the win margin
includeHTML(file.path("pages/margin_maps.html"))
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
output$statesimPlotly <- renderPlotly({
# calling function that I defined at the top of the app
pv2p_plot(input$state)
})
output$statevictoryPlotly <- renderPlotly(
# calling function from helper to make this plot
state_win_probs(input$state_type)
)
}
# Run the application
shinyApp(ui = ui, server = server)
正如我上面所说的,该函数在我的控制台中运行良好。大多数在线上对此有问题的人都没有使用正确的输出/渲染功能,例如使用renderPlot而不是renderpltly,但我看不出我的代码有什么问题。提前谢谢 检查您的选择输入。您可以简单地将plotlyoutputstate作为参数传递给selectInput@stefan我完全错过了那个精彩的节目&谢谢!不幸的是,我修复了这个问题,但仍然存在同样的问题。我不确定这是否是问题所在,在state_type输入中,您使用select而不是pull,因此您有一个data.frame作为choices@starja也很好,谢谢!不幸的是,这并没有解决问题,或者两个plotly输出都没有在my Shining Apps中呈现这不应该导致问题,但您可以/应该从函数中删除print语句。请检查selectInput。您可以简单地将plotlyoutputstate作为参数传递给selectInput@stefan我完全错过了那个精彩的节目&谢谢!不幸的是,我修复了这个问题,但仍然存在同样的问题。我不确定这是否是问题所在,在state_type输入中,您使用select而不是pull,因此您有一个data.frame作为choices@starja也很好,谢谢!不幸的是,这并没有解决问题,或者两个plotly输出都没有在my Shining Apps中呈现。这不应该导致问题,但您可以/应该从函数中删除print语句。