英文:
Rendering Plot and Table Within One Function
问题
I can help with the translation. Here's the translated text:
"我目前正在开发我的第一个Shiny Web应用程序。通过用户的文本输入,进行了一个MySQL查询,并从SQL数据库中检索数据。在我的代码片段中,我删除了查询以使其更加清晰。然后,使用这些数据创建了一个名为 df_meetdata
的Pandas数据框和一个图形 fig
。当前应用程序显示图形,但不显示数据框,因为在Shiny中不可能返回多个变量。有人知道如何同时显示图形和数据框吗?"
英文:
Currently i am working on my first webapplication in Shiny. Through text input from the user a MySQL query is being made and data is being retrieved from the SQL database. In my code snippet I removed the queries to keep it more clear. The data is then being used to create a pandas dataframe df_meetdata
and a graph fig
. Currently the app shows the graph but not the dataframe since it is not possible to return multiple variables in Shiny. Does someone know a way how I can show both the graph and dataframe?
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_slider("n", "N", 0, 100, 20),
ui.input_text("txt", "Referentienummer", value=""),
ui.input_action_button("run", "Run"),
ui.input_action_button("clear", "Clear"),
ui.input_action_button("export", "Export")
),
ui.panel_main(
ui.output_text("nmr"),
ui.output_text("print_referentienummer"),
ui.output_plot("get_absorptie_spectrum"),
ui.output_table("table_model"),
),
),
)
def server(input, output, session):
@output
@render.text
def nmr():
return f"input = {input.n()}"
@output
@render.text
def print_referentienummer():
return f"Referentienummer = {input.txt()}"
@output
@render.plot
@reactive.event(input.run, ignore_none=True)
def get_absorptie_spectrum():
# Ophalen datum meting + sensorID om absorptiewaarden uit de mysql db te kunnen halen
referentienummer = input.txt()
#SQL queries for retrieving the data#
df_meetdata = pd.DataFrame(
{"BlueBox": bluebox_naam, "Referentienummer": referentienummer, "Datum": date_time,
"ABS >= 2": max_absorptie}, index=[0])
fig, ax = plt.subplots()
ax.plot(golflengte, absorptie_waarden)
ax.set_title(referentienummer)
return fig
答案1
得分: 1
以下是您要翻译的内容:
"对于想要知道答案的人,我自己找到了:)。我查看了文档,找到了所需的函数,它是reactive.value()
函数。我创建了一个函数,用于从MySQL数据库中检索所有数据。我需要呈现的数据存储在变量中,并在单独的呈现函数中使用。
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_slider("n", "N", 0, 100, 20),
ui.input_text("txt", "Referentienummer", value=""),
ui.input_action_button("run", "Run"),
ui.input_action_button("clear", "Clear"),
ui.input_action_button("export", "Export")
),
ui.panel_main(
ui.output_plot("render_absorptie_spectrum"),
ui.output_table("render_bluebox_data"),
),
),
)
def server(input, output):
val_df_meetdata = reactive.Value()
val_absorptie_waarden = reactive.Value()
val_referentienummer = reactive.Value()
@reactive.Effect
@reactive.event(input.run)
def get_sql_data():
referentienummer = input.txt()
# SQL查询。结果存储在变量df_meetdata和absorptie_waarden中,这些变量在以下行中使用。#
val_df_meetdata.set(df_meetdata)
val_absorptie_waarden.set(absorptie_waarden)
val_referentienummer.set(referentienummer)
@output
@render.plot
def render_absorptie_spectrum():
absorptie_waarden = val_absorptie_waarden.get()
referentienummer = val_referentienummer.get()
fig, ax = plt.subplots()
ax.plot(golflengte, absorptie_waarden)
ax.set_title(referentienummer)
return fig
@output
@render.table
def render_bluebox_data():
df_meetdata = val_df_meetdata.get()
return df_meetdata
app = App(app_ui, server)
英文:
For the people that want to know the answer i found it myself :). I looked through the documentation and i found the function that was needed, it was the reactive.value() function. Ive created a function that retrieves all the data from the MySQL database. The data that I needed to render are being stored in variables and used in seperate render functions.
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_slider("n", "N", 0, 100, 20),
ui.input_text("txt", "Referentienummer", value=""),
ui.input_action_button("run", "Run"),
ui.input_action_button("clear", "Clear"),
ui.input_action_button("export", "Export")
),
ui.panel_main(
ui.output_plot("render_absorptie_spectrum"),
ui.output_table("render_bluebox_data"),
),
),
)
def server(input, output):
val_df_meetdata = reactive.Value()
val_absorptie_waarden = reactive.Value()
val_referentienummer = reactive.Value()
@reactive.Effect
@reactive.event(input.run)
def get_sql_data():
referentienummer = input.txt()
# SQL queries. The results are being stored in the variables df_meetdata and absorptie_waarden which are used in the following lines #
val_df_meetdata.set(df_meetdata)
val_absorptie_waarden.set(absorptie_waarden)
val_referentienummer.set(referentienummer)
@output
@render.plot
def render_absorptie_spectrum():
absorptie_waarden = val_absorptie_waarden.get()
referentienummer = val_referentienummer.get()
fig, ax = plt.subplots()
ax.plot(golflengte, absorptie_waarden)
ax.set_title(referentienummer)
return fig
@output
@render.table
def render_bluebox_data():
df_meetdata = val_df_meetdata.get()
return df_meetdata
app = App(app_ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论